package com.bnz.servlet;

import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.ClassesServce;
import com.bnz.service.StudentService;
import com.bnz.service.impl.ClassesServiceImpl;
import com.bnz.service.impl.StudentServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService;
    private ClassesServce classesServce;
    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
        classesServce = new ClassesServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
//        resp.setCharacterEncoding("text/html;charset=utf8"); 异步才使用

        String cmd = req.getParameter("cmd");
        if (StringUtils.isNotBlank(cmd)) {
            if ("list".equals(cmd)) {
                list(req, resp);
            } else if ("search".equals(cmd)) {// 条件查询带分页
                search(req, resp);
            } else if ("add".equals(cmd)) {
                add(req,resp);
            } else if ("update".equals(cmd)) {
                update(req, resp);
            } else if ("delete".equals(cmd)) {
                delete(req, resp);
            }
        }
    }




    // 1， 进行学生页面展示 将数据发送到jsp页面
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.1 得到数据
        List<Student> students = studentService.findAll();
        System.out.println("students = " + students);
        // 1.2 将数据存储到请求作用域中
        req.setAttribute("students", students);
        // 1.3 进行页面的转发 （转发是一次请求，所以可以在这次请求中得到上面的集合数据）
        req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);
    }
    // 2. 条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 2.1 查询所有的班级
        List<Classes> classes = classesServce.findAll();
        // 2.2 将所有班级放到作用域中
        req.setAttribute("classes",classes);
        // 2.3 得到学生查询的条件
        String sname = req.getParameter("sname");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");
        // 2.4 得到前端传入的分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        int page = new Integer(StringUtils.isNoneBlank(pageStr) ? pageStr : "1");
        int pageSize = new Integer(StringUtils.isNoneBlank(pageSizeStr) ? pageSizeStr : "8");
        // 2.5 将条件封装成对象
        StudentVo vo = new StudentVo(sname,addr, new Integer(StringUtils.isNoneBlank(cid) ? cid : "0"));
        // 开始查询
        PageResult<Student> pr = studentService.search(page, pageSize, vo);

        req.setAttribute("pr", pr);

        req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);


    }

    // 3.添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");

        Student student = new Student(sname, sex, new Integer(age), addr, new Integer(cid));

        studentService.insert(student);

        resp.sendRedirect("/student?cmd=search");
    }

    // 4.修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp)throws IOException{
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");

        Student student = new Student(new Integer(sid),sname,sex,new Integer(age),addr,new Integer(cid));
        // 修改
        studentService.updateById(student);
        resp.sendRedirect("/student?cmd=search");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        Integer sid = new Integer(req.getParameter("sid"));
        // 进行删除
        studentService.deleteById(sid);
        // 进行重定向
        resp.sendRedirect("student?cmd=search");
    }


}
